# -*- coding: utf-8 -*-
# __author__ = "sword"

if True:
    from os import path
    print('__file__=', __file__)
    parentPath = path.dirname(__file__)
    print('parentPath=', parentPath)
    toolPath = path.dirname(path.join(parentPath, 'tool/init.py'))
    print('toolPath=', toolPath)
    from sys import path
    path.append(toolPath)
    from init import *


def post_circulation(lines):
    import random
    round = 0
    while True:
        print('[+] -------- {} -------- '.format(round))

        tvText = poco("com.qidian.QDReader:id/tvText")
        if tvText.exists():
            tvText.click()
            print('[-] tvText')
            sleep(1.0)
        else:
            tvText = poco("com.qidian.QDReader:id/tvText")
            if tvText.exists():
                tvText.click()
                print('[-] tvText')
                sleep(1.0)

        edittext = poco("com.qidian.QDReader:id/edittext")
        if edittext.exists():
            edittext.click()
            print('[-] edittext')
            sleep(1.0)
        else:
            edittext = poco("com.qidian.QDReader:id/edittext")
            if edittext.exists():
                edittext.click()
                print('[-] edittext')
                sleep(1.0)

        print('[-] text')
        text(lines[i % len(lines)])
        sleep(2.0)

        submitBtn = poco("com.qidian.QDReader:id/submitBtn")
        if submitBtn.exists():
            submitBtn.click()
            print('[-] over')
            sleep(15)
        else:
            submitBtn = poco("com.qidian.QDReader:id/submitBtn")
            if submitBtn.exists():
                submitBtn.click()
                print('[-] over')
                sleep(15)

        round += 1


def get_post():
    # try:
    #     while True:
    #         print('swipe')
    #         poco.swipe([0.5,0.8], [0.5,0.2],duration=0.05)
    # except:
    #     pass

    names = []
    i = 0
    while True:

        parent = poco('com.qidian.QDReader:id/qd_recycler_view')
        last_2nd_child = parent.child()[3]
        name = last_2nd_child.offspring(
            'com.qidian.QDReader:id/user_name').attr('text')
        i += 1
        print(name, i)
        names.append(name + '\n')
        last_2nd_child.drag_to([0.2361111111111111, 0.834375], duration=0.5)
        with open('file.txt', 'w') as fp:
            fp.writelines(names)

    print('over')


def email():
    import yagmail
    user = '1084662708@qq.com'
    # 得到的授权码，而非密码
    password = 'kracwpbojhbvjigb'
    receiver = '1084662708@qq.com'
    # 字符自动是正文中，r表示文件自动归档到附件里。
    contents = ['exception']
    email = yagmail.SMTP(user=user, password=password, host='smtp.qq.com')
    email.send(to=receiver, subject='reply', contents=contents)
    email.close()
    print('exception')


if __name__ == '__main__':
    lines = None
    with open('道德经txt.txt', 'r', encoding='utf-8') as fp:
        lines = fp.readlines()

    try:
        post_circulation(lines)
        # get_post()
    except:
        email()
